package com.togglz.example.togglzexample;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.togglz.core.activation.UsernameActivationStrategy;
import org.togglz.core.manager.FeatureManager;
import org.togglz.core.spi.ActivationStrategy;
import org.togglz.core.user.SimpleFeatureUser;

@RestController
@RequestMapping(value = "togglz")
public class TogglZController {

    @Autowired
    private FeatureManager manager;


    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public ResponseEntity<?> index(@RequestParam(value = "name", required = false) String name) {
        for (ActivationStrategy activationStrategy : manager.getActivationStrategies()) {
            if (activationStrategy instanceof UsernameActivationStrategy) {
                UsernameActivationStrategy usernameActivationStrategy = (UsernameActivationStrategy) activationStrategy;
                SimpleFeatureUser simpleFeatureUser = new SimpleFeatureUser(name);
                if (usernameActivationStrategy.isActive(manager.getFeatureState(MyFeatures.FEATURE_ONE), simpleFeatureUser)) {
                    return new ResponseEntity<String>("FEATURE_ONE", HttpStatus.OK);
                } else if (usernameActivationStrategy.isActive(manager.getFeatureState(MyFeatures.FEATURE_TWO), simpleFeatureUser)) {
                    return new ResponseEntity<String>("FEATURE_TWO", HttpStatus.OK);
                }
            }
        }


        return new ResponseEntity<String>("NOT FOUND", HttpStatus.NOT_FOUND);
    }

}
